package com.example.base.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface DBMapper extends BaseMapper {

    /**
     * 查询当前使用的库名
     * @return
     */
    @Select("SELECT DATABASE()")
    String selectDataBaseName();

    /**
     * 查询当前系统的表及表名
     * @param dataBaseName 库名
     * @return
     */
    @Select("SELECT table_name, table_comment FROM information_schema.tables WHERE table_schema = #{dataBaseName}")
    List<Map<String,String>> selectAllTables(@Param("dataBaseName")String dataBaseName);

    /**
     * 为系统表添加注释
     * @param tableName  表名
     * @param commentName 注释名
     * @return
     */
    @Update("ALTER TABLE ${tableName} COMMENT '${commentName}'")
    int updateTableComment(@Param("tableName")String tableName,@Param("commentName") String commentName);


    @Select(" SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE, COLUMN_KEY, COLUMN_COMMENT " +
            " FROM INFORMATION_SCHEMA.COLUMNS " +
            " WHERE TABLE_SCHEMA = #{dataBaseName} AND TABLE_NAME = #{tableName} ")
    List<Map<String,Object>> selectTableInfo(@Param("dataBaseName")String dataBaseName,@Param("tableName")String tableName);


    @Select("${sql}")
    List<Map<String,Object>> selectListBySql(@Param("sql")String sql);

    @Select("${sql}")
    Map<String,Object> selectObjBySql(@Param("sql")String sql);

}
